package vn.com.pavibook.admin.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import vn.com.pavibook.domain.model.SupportNews;
import vn.com.pavibook.model.vo.SupportNewsVO;
import vn.com.pavibook.service.SupportNewsService;

@Controller
public class ManageSupportNewsController {

	@Autowired
	private SupportNewsService supportNewsService;
	
	@RequestMapping(value="admin/manage-support-news", method=RequestMethod.GET)
	public String manageSupportNews(Model model) {
		List<SupportNews> bunchOfSupportNews= supportNewsService.getAllSupportNews();
		model.addAttribute("supportNews", bunchOfSupportNews);
		return "admin/manage-support-news";
	}
	
	@RequestMapping(value="admin/add-support-news", method=RequestMethod.GET)
	public String addSupportNews(Model model){
		SupportNewsVO supportNews = new SupportNewsVO();
		model.addAttribute("supportNews", supportNews);
		return "admin/add-support-news";
	}
	
	@RequestMapping(value="admin/addSupportNews", method=RequestMethod.POST)
	public String saveSupportNews(Model model, @ModelAttribute("supportNews") @Valid SupportNewsVO supportNewsVO, BindingResult result, HttpServletRequest request) {
		if(result.hasErrors()){
			return "admin/add-support-news";
		}
		boolean resultUpload = supportNewsService.uploadImages(model, supportNewsVO, request);
		if(!resultUpload){
			return "admin/add-support-news";
		} else {
			SupportNews supportNews = supportNewsVO.convertToSupportNews();
			supportNewsService.save(supportNews);
		}
		return "redirect:manage-support-news"; 
	}
	
	@RequestMapping(value="admin/delete-supportNews", method=RequestMethod.GET)
	public String deleteSupportNews(Model model, @RequestParam("id") int id) {
		supportNewsService.deleteSupportNewsById(id);
		return "redirect:manage-support-news"; 
	}
	
	@RequestMapping(value="admin/edit-supportNews", method=RequestMethod.GET)
	public String editSupportNews(Model model, @RequestParam("id") int id) {
		SupportNews supportNews = supportNewsService.getSupportNewsById(id);
		SupportNewsVO supportNewsVO = supportNews.convertToSupportNewsVO(); 
		model.addAttribute("supportNews", supportNewsVO);
		return "admin/add-support-news";
	}
	
	@RequestMapping(value="admin/ajax/viewContentSupportNews", method=RequestMethod.GET)
	public ModelAndView viewDetailBanner(@RequestParam("id") int id){
		ModelAndView model = new ModelAndView("admin/view-content-support-news");
		SupportNews supportNews = supportNewsService.getSupportNewsById(id);
		model.addObject("supportNews", supportNews);
		return model;
	}
	
}
